﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using MSBTC.Model;
using MSBTC.ReportViewer;

namespace MSBTC.WinForms
{
    public partial class RequisitionForm : Form
    {
        PMSContext dbcontext = new PMSContext();
        Requisition dbreq = new Requisition();
        BindingSource bsrc = new BindingSource();

        private int ans = 0;
        private int cnt = 0;
        private int totcost = 0;

        public RequisitionForm()
        {
            InitializeComponent();
        }

        private void Requisition_Load(object sender, EventArgs e)
        {
            cmbProj.SelectedIndex = 0;

            var proj = (from s in dbcontext.Projects
                        select s).ToList();
            if (proj.Count != 0)
            {
                foreach (var itm in proj)
                {
                    cmbProj.Items.Add(itm.Project_Name);
                }
            }
            else
            {
                MessageBox.Show("There are no Projects retrieve, Please click \"New\" to create new Project", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            var rq = (from r in dbcontext.Requests
                      select r).ToList();
            if (rq.Count != 0)
            {
                foreach (var itm in rq)
                {
                    cnt = itm.Requisitionid;
                }
                lblReq.Text = Convert.ToString(cnt + 1);
            }       
            else
            {
                lblReq.Text = "1";
            }
            lblTot.Text = "0";
            
            int rid = Convert.ToInt32(lblReq.Text);
            var req = (from r in dbcontext.Requests
                       where r.Requisitionid == rid
                       select r).ToList();
            if (req.Count != 0)
            {
                bsrc.DataSource = req;
                dgvReq.DataSource = bsrc;
            }

            DataGridViewComboBoxColumn cmbMname = new DataGridViewComboBoxColumn();
            DataGridViewComboBoxColumn cmbUnit = new DataGridViewComboBoxColumn();
            cmbUnit = (DataGridViewComboBoxColumn)dgvReq.Columns["unit"];
            cmbMname = (DataGridViewComboBoxColumn)dgvReq.Columns["mname"];

            var matr = (from mat in dbcontext.Mat_Maintenances
                        select mat).ToList();
            if (matr.Count != 0)
            {
                foreach (var itms in matr)
                {
                    cmbMname.Items.Add(itms.Material_Name.ToString()); 
                }
            }

            var unts = (from u in dbcontext.Units
                        select u).ToList();
            if (unts.Count != 0)
            {
                foreach (var itm in unts)
                {
                    cmbUnit.Items.Add(itm.Units.ToString());
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //print();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDate.Clear();
            txtOrder.Clear();
            cmbProj.SelectedIndex = 0;
            lblTot.Text = "0";

            foreach (DataGridViewRow row in dgvReq.SelectedRows)
                if (!row.IsNewRow) dgvReq.Rows.Remove(row);
            
        }

        private void LLNewS_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProjectInfoForm Pform = new ProjectInfoForm();

            Pform.Show();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            dgvReq.Rows.Add(new object[] { null, null });
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(lblReq.Text);
            var req = (from r in dbcontext.Requests
                       where r.Requisitionid == id
                       select r).ToList();
            if (req.Count == 0)
            {
                for (int i = 0; i <= dgvReq.Rows.Count-2; i++)
                {
                    dbreq.Requisitionid = id;
                    dbreq.ReqBy = txtOrder.Text;
                    dbreq.Date = txtDate.Text;
                    var pj = (from p in dbcontext.Projects
                              where p.Project_Name == cmbProj.SelectedItem
                              select p).SingleOrDefault();
                    if (pj != null)
                    {
                        dbreq.Projectid_FK = pj.Projectid_PK;
                    }
                    else
                    {
                        MessageBox.Show("Please select Project Name to assign the materials where to deliver", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    dbreq.MaterialName = dgvReq.Rows[i].Cells["mname"].Value.ToString();
                    dbreq.Unit = dgvReq.Rows[i].Cells["unit"].Value.ToString();
                    dbreq.Quantity = Convert.ToInt32(dgvReq.Rows[i].Cells["qty"].Value);
                    dbreq.UnitCost = Convert.ToInt32(dgvReq.Rows[i].Cells["ucost"].Value);

                    dbcontext.Entry(dbreq).State = EntityState.Added;
                    dbcontext.SaveChanges();

                    MessageBox.Show("Successfully saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    var rq = (from r in dbcontext.Requests
                              select r).ToList();
                    if (rq.Count != 0)
                    {
                        foreach (var itm in rq)
                        {
                            cnt = itm.Requisitionid;
                        }
                        lblReq.Text = Convert.ToString(cnt + 1);
                    }
                    else
                    {
                        lblReq.Text = "1";
                    }
                }
            }
        }

        private void dgvReq_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
            
        }

        private void dgvReq_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int r = e.RowIndex;
            lblTot.Text = "";

            totcost += (Convert.ToInt32(dgvReq.Rows[r].Cells["ucost"].Value) * Convert.ToInt32(dgvReq.Rows[r].Cells["qty"].Value)) ;

            lblTot.Text = totcost.ToString("N");
        }

        private void btnrRem_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvReq.SelectedRows.Count > 0)
                {
                    dgvReq.Rows.RemoveAt(dgvReq.SelectedRows[0].Index);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + ". The row your've selected is an empty row which cannot be deleted try tp select row with some infomation", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void btnRAll_Click(object sender, EventArgs e)
        {
           
            
        }

    }
}